<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑债务': '新增债务'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="类型">
          <a-select v-decorator="['type', { rules: [{ required: true, message: '请选择' }] }]" placeholder="请选择 ">
            <a-select-option value="0">欠账</a-select-option>
            <a-select-option value="1">清账</a-select-option>
            <a-select-option value="2">借钱</a-select-option>
            <a-select-option value="3">还钱</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="记账账户" >
          <a-select v-decorator="['recordAccId', { rules: [{ required: true, message: '请选择' }] }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in this.option" :key="i.value">{{ i.accName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="金额">
          <a-input addonAfter="元" placeholder="请输入金额" v-decorator="['amount', { rules: [{ required: true, message: '请输入金额' }] }]" />
        </a-form-item>
        <a-form-item label="记账日期">
          <a-date-picker v-decorator="['date', { rules: [{ required: true, message: '请选择' }] }]" style="width: 100%" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea rows="2" v-decorator="['memo']" placeholder="请输入备注"/>
        </a-form-item>
        <a-form-item v-if="model && model.id > 0" label="账户状态">
          <a-select v-decorator="['status']" placeholder="请选择">
            <a-select-option value="1">可欠账</a-select-option>
            <a-select-option value="2">已停用</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
  
  <script>
  import pick from 'lodash.pick'
  import { getStAccountListByTypeUsingGET } from '@/api/api/qiantaizhangwu.ts'
  
  // 表单字段
  const fields = ['type','recordAccId','amount','date', 'memo', 'status', 'id']
  
  export default {
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      model: {
        type: Object,
        default: () => null
      }
    },
    data () {
      this.formLayout = {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 }
        }
      }
      return { 
        queryParam: {
          pageNo: 1,
          pageSize: 999,
          queryCondition: '',
          status: 1,
        },
        option:[],
        form: this.$form.createForm(this)
      }
    },
    created () {
      this.handleStore()
      // 防止表单未注册
      fields.forEach(v => this.form.getFieldDecorator(v))
  
      // 当 model 发生改变时，为表单设置值
      this.$watch('model', () => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    },
    methods:{
      handleStore(){
        getStAccountListByTypeUsingGET({...this.queryParam,typeList:'0,1'}).then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
      },
    }
  }
  </script>